<template>
  <div class="add-condition">
    <CommonList class="list"
                :items="listData"
                :grouping="false"
                @use="onUse"
                title="从以下位置选择一个对象以创建新条件"></CommonList>
    <div class="buttons">
      <SButton @click="onGoNext">继续</SButton>
    </div>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, inject } from 'vue';
import CommonList from '../common-list/list.vue';
import SButton from '@/ui-kit/button/button.vue';
import { ListItem } from '../common-list';
import assetsStore from '../../../store/assets';
import { AssetType, IAssetInfo } from '../../../store/types';
import { ProvideKey } from '@/ui-kit/window';
import pluginsStore from '../../../store/plugins';

export default defineComponent({
  name: 'add-condition',
  components: { SButton, CommonList },
  emits: [],
  props: {},
  setup(props, { emit }) {
    const responseCB = inject(ProvideKey.ResponseCB, (data: ListItem) => {});
    const closeWindow = inject(ProvideKey.CloseWindow, ()=>{});
    const listData = computed<Array<ListItem>>(() => {
      const objects = assetsStore().filterAssetByType(AssetType.Object);
      const items: ListItem[] = objects.map((obj: IAssetInfo) => {
        let a: ListItem = {
          data: obj.id,
          group: 'object',
          label: obj.name,
          icon: obj.icon || 'icon-unknow',
          desc: obj.name,
        };
        return a;
      });

      // 不能实例化的插件对象也是条件对象
      const plugins: ListItem[] = [];
      const arr = pluginsStore().getCannotInstantiateGameObject();
      arr.forEach((obj: sail.GameObject) => {
        const { group, name, icon, desc } = obj.info;
        plugins.push({
          data: obj.bundle,
          group,
          label: name,
          icon,
          desc,
        });
      });

      return plugins.concat(items);
    });
    return {
      listData,
      onGoNext() {

      },
      onUse(data: ListItem) {
        responseCB(data);
        closeWindow();
      },
    };
  },
});
</script>
<style scoped lang="scss">
.add-condition {
  padding: 5px;
  display: flex;
  flex-direction: column;
  width: 100%;

  .list {
    flex: 1;
  }

  .buttons {
    display: flex;
    flex-direction: row;

  }
}
</style>
